#!/usr/bin/perl

#OBJECT DEFINITION
package AcessDB;

# PERL MODULES WE WILL BE USING
use DBI;
use DBD::mysql;

# ALLOCATE VARIABLE
sub new {
	my $self = {};
	bless $self;
	$self->{PLATFORM};
	$self->{DATABASE};
	$self->{HOST};
	$self->{PORT};
	$self->{TABLENAME};
	$self->{USER};
	$self->{PW};
	#warn "CREATING " . \$self;
	return bless $self;
}

# DESALLOCATE VARIABLES
sub DESTROY {
	my $self = shift;
	warn "DESTROYING $self";
}

# CONECT TO THE DB
sub ConnectDB {
	my $self = shift;
	$self->Reconfigure( $self, "mysql", shift, shift, shift, shift, shift, "NoTable");

	#DATA SOURCE NAME
	$dsn = "dbi:"
	  . $self->{PLATFORM} . ":"
	  . $self->{DATABASE} . ":"
	  . $self->{HOST} . ":"
	  . $self->{PORT};
	print "dsn = " . $dsn . "\n";

	# PERL DBI CONNECT (RENAMED HANDLE)
	$dbdatachk = DBI->connect( $dsn, $self->{USER}, $self->{PW} )
	  or die "Unable to connect: $DBI::errstr\n";
}

# Conect to the DB using one table
sub ConnectDB {
	my $self = shift;
	$self->Reconfigure( $self, "mysql", shift, shift, shift, shift, shift, shift );

	#DATA SOURCE NAME
	$dsn = "dbi:"
	  . $self->{PLATFORM} . ":"
	  . $self->{DATABASE} . ":"
	  . $self->{HOST} . ":"
	  . $self->{PORT};
#	print "dsn = " . $dsn . "\n";

	# PERL DBI CONNECT (RENAMED HANDLE)
	$dbdatachk = DBI->connect( $dsn, $self->{USER}, $self->{PW} )
	  or die "Unable to connect: $DBI::errstr\n";
}


# CONFIG VARIABLES
sub Reconfigure {
	my $self = shift;
	$a                 = shift;    #????????????
	$self->{PLATFORM}  = shift;
	$self->{DATABASE}  = shift;
	$self->{HOST}      = shift;
	$self->{PORT}      = shift;
	$self->{USER}      = shift;
	$self->{PW}        = shift;
	$self->{TABLENAME} = shift;
#print "Reconfig:".$self->{PLATFORM}.":".$self->{DATABASE}.":".$self->{HOST}.":".$self->{PORT}.":".$self->{USER}.":".$self->{PW}.":".$self->{TABLENAME}."\n";
}

# RECEIVE QUERYS
sub Query {
	my $self = shift;
	$self->{QUERY} = shift;

	# PREPARE THE QUERY STRING
	$query_handle = $dbdatachk->prepare( $self->{QUERY} );

	# EXECUTE THE QUERY
	$query_handle->execute();
	return $query_handle;
}

sub listtables{
	return $dbdatachk->tables();
}

sub name {
	my $self = shift;
	if (@_) { $self->{NAME} = shift }
	return $self->{NAME};
}
1;
